<?php
    session_start();
    require_once '../includes/runtime/lib/Propel.php';
    Propel::init("../includes/build/conf/easylib-conf.php");
    set_include_path("../includes/build/classes" . PATH_SEPARATOR . get_include_path());
    if(isset($_SESSION["admin_login"])&&isset($_SESSION["admin_key"])){
        $query = TabAdminQuery::create()->filterByLogin($_SESSION["admin_login"])->findOne();
        if($query!=null){
            if(md5($query->getPass())==$_SESSION["admin_key"]){
                switch ($_GET["action"]){
                    case "liste":
                        $repartitions = TabRepartitionQuery::create()->filterByFkAdmin($query->getIdAdmin())->find();
                        $results = array();
                        foreach ($repartitions as $repartition){
                            $repartition instanceof TabRepartition;
                            $date = $repartition->getDateRepartition(null);
                            $minute = intval($date->format("i")) +60;
                            $heure = intval($date->format("H"));
                            if($minute==60){
                                $minute = 0;
                                $heure++;
                            }
                            $date->setTime($heure, $minute, 0);
                            $results[] = array(
                                'id' => $repartition->getIdRepartition(),
                                'title' => $repartition->getTabStation()->getStationName(),
                                'start' => $repartition->getDateRepartition(),
                                'end' => $date->format("Y-m-d H:i:s"),
                                'allDay' => false
                            );
                        }
                        echo json_encode($results);
                        break;
                    case "generate":
                        $date = new DateTime();
                        if($date->format("N")==6){
                            $date->add(new DateInterval("P2D"));
                        }
                        if($date->format("N")==7){
                            $date->add(new DateInterval("P1D"));
                        }
                        $tmp_test = TabRepartitionQuery::create()->filterByFkAdmin($query->getIdAdmin())->where(TabRepartitionPeer::DATE_REPARTITION." like '".$date->format("Y-m-d")." %:%:%'")->limit(8)->findOne();
                        if($tmp_test != null){
                            break;
                        }
                        $repartitions = TabRepartitionQuery::create()->filterByFkAdmin(null)->filterByStatut(0)->limit(10)->find();
                        $range = array();
                        $ordre_tmp = array();
                        $tmp = 0;
                        foreach($repartitions as $repartition){
                            $ordre_tmp[] = $tmp;
                            $tmp++;
                            $range[] = array(
                                "i" => $tmp,
                                "latitude" => $repartition->getTabStation()->getStationLatitude()*1000,
                                "longitude" => $repartition->getTabStation()->getStationLongitude()*1000
                            );
                        }
                        $ordre = array();
                        $ordre[] = 0;
                        $ordre_tmp[0] = null;
                        //var_dump($range);
                        for($i=1;$i<count($range);$i++){
                            $x1 = $range[$ordre[$i-1]]["latitude"];
                            $y1 = $range[$ordre[$i-1]]["longitude"];
                            $sup_i = $i;
                            $d1 = null;
                            //var_dump($ordre_tmp);
                            //echo "<br /> <br />";
                            for($j=1;$j<count($ordre_tmp);$j++){
                                if($ordre_tmp[$j]!=null){
                                    $x2 = $range[$ordre_tmp[$j]]["latitude"];
                                    $y2 = $range[$ordre_tmp[$j]]["longitude"];
                                    $d2 = (pow($x2-$x1,2) + pow($y2-$y1,2));
                                    //echo $d2."<br />";
                                    if($d1==null||$d2<$d1){
                                        //echo "------>".$d2."<br />";
                                        $d1 = ($d2);
                                        $sup_i = $j;
                                    }
                                }
                            }
                            $ordre[] = $ordre_tmp[$sup_i];
                            //echo "Choisi : ".$ordre_tmp[$sup_i]."<br />";
                            $ordre_tmp[$sup_i] = null;
                        }
                        //var_dump($ordre);
                        $date->setTime(8, 0, 0);
                        for($i=0;$i<$repartitions->count()/2;$i++){
                            $repartition = $repartitions->get($ordre[$i]);
                            $repartition->setDateRepartition($date);
                            $repartition->setFkAdmin($query->getIdAdmin());
                            $repartition->save();
                            $date->add(new DateInterval("PT60M"));
                        }
                        $date->setTime(14, 0, 0);
                        for($i=$repartitions->count()/2;$i<$repartitions->count();$i++){
                            $repartition = $repartitions->get($ordre[$i]);
                            $repartition->setDateRepartition($date);
                            $repartition->setFkAdmin($query->getIdAdmin());
                            $repartition->save();
                            $date->add(new DateInterval("PT60M"));
                        }
                        break;
                }
            }
        }
    }
?>
